package com.study.bigwork.circle.controller;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.study.bigwork.circle.service.CircleService;
import com.study.bigwork.entity.Circle;
import com.study.bigwork.entity.CircleComment;
import com.study.bigwork.entity.MomentComment;


@Controller
@RequestMapping("/circle")
public class CircleController { 
	@Resource
	private CircleService circleService;
	
	
	//获取朋友圈的所有评论
	@RequestMapping(value="/    ",method=RequestMethod.GET)
	public void getCircleComment(HttpServletRequest request,HttpServletResponse response)throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				//设置返回数据格式和编码
		        response.setContentType("application/json;charset=utf-8");
		        //定义StringBuffer变量
		        String responseMessage = null;
		        
		        //输出流
		        PrintWriter out = response.getWriter();
		        String id = request.getParameter("id");
		        String sql = "select * from `circle_comment` where circle_id='"+id+"'";
		        List<CircleComment> circleComments= circleService.getCircleComments(Integer.parseInt(id));
		      
		        System.out.println("666"+Arrays.asList(circleComments));
		        Gson gson = new Gson();
		        responseMessage = gson.toJson(circleComments);
		        System.out.println("comment对象转为json " + responseMessage);
		        //输出流将信息返回
		        out.print(responseMessage);
	}
	
	//删除朋友圈的评论
	@RequestMapping(value="/    ",method=RequestMethod.POST)
	public void deleteCircleComment(HttpServletRequest request,HttpServletResponse response)throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				// 设置返回数据格式和编码
				response.setContentType("application/json;charset=utf-8");
				
				//客户端获取id
				String id = request.getParameter("id");
				//获取输出流
				PrintWriter out = response.getWriter();
				//获取朋友圈服务。
				
				int circleId=circleService.selectCircleIdById(Integer.parseInt(id));
				int flag = circleService.deleteCommentById(Integer.parseInt(id));
				
				circleService.reduceCommentSize(circleId);
			
				out.print(flag);
	}
	
	public void uploadCircleComment(HttpServletRequest request,HttpServletResponse response)throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				// 设置返回数据格式和编码
				response.setContentType("application/json;charset=utf-8");
				
				//获取输出流
				PrintWriter out = response.getWriter();
				//接收客户端上传的评论相关信息
				String circle_id = request.getParameter("id");
				String comment_id = request.getParameter("userId");
				String content = request.getParameter("comment");
				System.out.println(circle_id+comment_id+content);
				
				
			    boolean flag = circleService.addCircleComment(Integer.parseInt(circle_id), Integer.parseInt(comment_id), content);
			    circleService.addCommentSize(Integer.parseInt(circle_id));
			    out.print(flag);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	@RequestMapping(value="/addCircle",method=RequestMethod.POST)
	public void addCircle(HttpServletRequest request,HttpServletResponse response)throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				//设置返回数据格式和编码
		        response.setContentType("application/json;charset=utf-8");
		        
		        //定义StringBuffer变量
		      	StringBuffer stringBuffer = new StringBuffer();
		        //line保存读取请求信息的当前一行，responseMessage为响应信息，返回信息
		        String line = null, responseMessage = null;
		        
		        //输出流
		        PrintWriter out = response.getWriter();
		        
		        //读取信息时会发生IO异常
		        try{
		            //BufferedReader为缓冲读取流
		            BufferedReader bufferedReader = request.getReader();
		            while((line = bufferedReader.readLine()) != null){
		                stringBuffer.append(line);
		            }
		            System.out.println(stringBuffer);
		            
		        }catch(IOException e){
		            e.printStackTrace();
		        }
		        
		        //将json数据转为String
		        Gson gson = new Gson();
		        Circle circle =gson.fromJson(stringBuffer.toString(), Circle.class);
		        boolean b = false;
		        System.out.println(circle.toString());
		        //调用CircleService类中addCircle方法访问数据库，并返回查询结果
		       
		        b = circleService.addCircle(circle);
		        responseMessage = gson.toJson(b);
		        
		        System.out.println("getCircles    对象转为json " + responseMessage);
		        //输出流将信息返回
		        out.print(responseMessage);
		
	}
	@RequestMapping(value="/getCircles",method=RequestMethod.GET)
	public void getCircles(HttpServletRequest request,HttpServletResponse response)throws IOException{
		// 设置编码方式
				request.setCharacterEncoding("utf-8");
				//设置返回数据格式和编码
		        response.setContentType("application/json;charset=utf-8");

				//定义StringBuffer变量
		        String responseMessage = null;
		      
		        //输出流
		        PrintWriter out = response.getWriter();
		        String homeworkType = "";
		        String homeworkTypes = "";
		        String tag = "";
		        String tags = "";
		        String sql = "select * from `circle` order by id DESC";
		        
		        
		        //调用HomeworkService类中getHomeworks方法访问数据库，并返回查询结果
		       List<Circle> circles=new ArrayList<Circle>(); 
		        circles = circleService.getCircles();
		        System.out.println("getCircles    "+Arrays.asList(circles));
		        Gson gson = new Gson();
		        responseMessage = gson.toJson(circles);
		        System.out.println("getCircles   对象转为json " + responseMessage);
		        //输出流将信息返回
		        out.print(responseMessage);
	}
	@RequestMapping(value="/uploadCircleImage/{imageName}",method=RequestMethod.POST)
	public void uploadCircleImage(@PathVariable("imageName") String imageName,HttpServletRequest request,HttpServletResponse response)throws IOException{
	
	// 设置编码方式
			request.setCharacterEncoding("utf-8");
			// 设置返回数据格式和编码
			response.setContentType("application/json;charset=utf-8");

			// 接收客户端上传的文件
			
			System.out.println("uploadCircleImage     "+imageName);
			String address = request.getServletContext().getRealPath("/");
			String path = "./circleImage/" + imageName+".jpg";
			InputStream in = request.getInputStream();
			FileOutputStream fos = new FileOutputStream(address + path);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = in.read(buffer)) != -1) {
				fos.write(buffer, 0, len);
			}
			fos.flush();
			fos.close();
			in.close();
			response.getWriter().append("上传成功");

	}
	

}
